﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Windows.Forms;
using System.Globalization;

namespace GRTVPluginConsole.Listener.Processors
{
    class WeatherServerDataProcessor
    {
        Form1 callback;
        private byte[] data;
        public WeatherServerDataProcessor(IPEndPoint ep, byte[] data, Form1 callback)
        {
            this.data = data;
            this.callback = callback;
        }

        public void ThreadLoop(Object threadContext)
        {
            if (callback != null)
            {
                if (data[1] == 0)
                {
                    // plugin port (id) unknown
                    callback.Invoke((MethodInvoker)delegate
                    {
                        callback.lblWeatherMessage.Text = "No weather plugin with identifier: " + callback.txtrF2WSPluginPort.Text;
                    });
                }
                else if (data[1] == 1)
                {
                    // plugin port (id) unknown
                    callback.Invoke((MethodInvoker)delegate
                    {
                        callback.lblWeatherMessage.Text = "Weather plugin " + callback.txtrF2WSPluginPort.Text + " is not running. Retry later.";
                    });
                }
                else if (data[1] == 2)
                {
                    Program.configuration.realtimeConfig.ownLatitude = BitConverter.ToDouble(data, 2);
                    Program.configuration.realtimeConfig.ownLongitude = BitConverter.ToDouble(data, 10);
                    callback.Invoke((MethodInvoker)delegate
                    {
                        callback.lblWeatherMessage.Text = "Track location: lat=" + Program.configuration.realtimeConfig.ownLatitude + " / lon=" + Program.configuration.realtimeConfig.ownLongitude;
                        Program.configuration.realtimeConfig.StartOrRestartTimer();
                        callback.btCallrF2WS.Enabled = false;
                        callback.btStopCollectRadar.Enabled = true;
                        callback.lblRadarCoordinate.Text = "lat= " + Program.configuration.realtimeConfig.ownLatitude.ToString(CultureInfo.InvariantCulture) + " / lon= " + Program.configuration.realtimeConfig.ownLongitude.ToString(CultureInfo.InvariantCulture) + "";
                    });
                }
            }
        }
    }
}
